package com.example.service;

import com.example.entity.Film;
import com.example.mapper.FilmMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FilmService {

    @Resource
    private FilmMapper filmMapper;

    public PageInfo<Film> selectPage(Film film, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Film> list = filmMapper.selectAll(film);
        return PageInfo.of(list);
    }

    //获取用户推荐
    public List<Film> selectByUser(Integer id){
        return filmMapper.selectByUserId(id);
    }

    //获取用户评论
    public List<Film> selectByComment(Integer id){
        return filmMapper.selectByCommentId(id);
    }

    public Film selectById(Integer id){
        return filmMapper.selectById(id);
    }

    // 获取推荐电影列表
    public List<Film> selectRecommend(Integer id){
        List<Film> list = filmMapper.selectAll(null);
        // 排除当前电影
        list = list.stream().filter(film -> !film.getId().equals(id)).collect(Collectors.toList());
        // 打乱顺序
        Collections.shuffle(list);
        return list;
    }

    public void add(Film film){
        filmMapper.add(film);
    }

    public void update(Film film){
        filmMapper.update(film);
    }

    public void delete(Integer id){
        filmMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            filmMapper.delete(id);
        }
    }

    // 获取电影排行榜
    public List<Film> getFilmRecommend() {
        List<Film> filmList = filmMapper.sortTotal();
        return filmList;
    }
}
